/*
 * Copyright (C), 2002-2016, 上海亦寻信息技术有限公司
 * FileName: SellControlServiceImp.java
 * Author:   linyao
 * Date:     2016-7-5 上午11:31:39
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.emm.yixun.website.service.marketing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.common.model.PriceSchemeDetail;
import com.emm.yixun.common.model.dto.SellControlDto;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.utils.DateStyle;
import com.emm.yixun.common.utils.DateUtils;
import com.emm.yixun.marketing.service.PriceSchemeDetailServiceFacade;
import com.emm.yixun.marketing.service.SellControlServiceFacade;

/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * 
 * @author linyao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class SellControlServiceImp implements SellControlService {

	@Autowired
	private SellControlServiceFacade sellControlServiceFacade;
	@Autowired
	private PriceSchemeDetailServiceFacade schemeDetailServiceFacade;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.emm.yixun.website.service.marketing.SellControlService#findPageByDto(com.emm.yixun.common.model.dto.
	 * SellControlDto, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public Response<Page<SellControlDto>> findPageByDto(SellControlDto reqDto, Integer pageSize, Integer pageNum) {
		if (StringUtils.isNotBlank(reqDto.getM_businessStatus())) {
			String sql = new String(" and t.control_type in (");
			String[] status = reqDto.getM_businessStatus().split(",");
			String sbsql = "";
			for (int i = 0; i < status.length; i++) {
				sbsql += status[i];
				if (i != status.length - 1) {
					sbsql += ",";
				}
			}
			sql += sbsql + ")";
			reqDto.setM_businessStatus(sql);
		}
		Response<Page<SellControlDto>> response = sellControlServiceFacade.getSellControlDtoList(reqDto, pageSize,
				pageNum);
		if (response.isSuccess() && response.getEntity() != null && response.getEntity().getRows() != null
				&& response.getEntity().getRows().size() > 0) {
			List<SellControlDto> controlDtos = response.getEntity().getRows();
			for (SellControlDto controlDto : controlDtos) {
				Response<PriceSchemeDetail> response2 = schemeDetailServiceFacade.selectEntityByRoomId(controlDto
						.getRoomId());
				if (response2.isSuccess() && response2.getEntity() != null) {
					controlDto.setTotalPrice(response2.getEntity().getTotalPrice());
				}
			}

		}
		return response;
	}

	@Override
	public List<SellControlDto> querySellListExport(SellControlDto reqDto) {
		reqDto.setIsExport("1");
		Response<Page<SellControlDto>> page = findPageByDto(reqDto, 10, 0);
		return page.getEntity().getRows();
	}

	@Override
	public List<Map<String, String>> returnFieldSell(List<SellControlDto> list,String[] header) {
		List<Map<String, String>> listMap = new ArrayList<Map<String,String>>();
		for (int i=0;i<list.size();i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			SellControlDto sell = list.get(i);
			map.put(header[0]+"_"+i, DateUtils.DateToString(sell.getControlDate(), DateStyle.YYYY_MM_DD.getValue()));
			map.put(header[1]+"_"+i, sell.getCombinedName());
			map.put(header[2]+"_"+i,sell.getHouseTypeName());
			map.put(header[3]+"_"+i,sell.getTotalPrice()==null?"":sell.getTotalPrice().toString());
			map.put(header[4]+"_"+i,DateUtils.DateToString(sell.getStartValidity(), DateStyle.YYYY_MM_DD.getValue()));
			map.put(header[5]+"_"+i,DateUtils.DateToString(sell.getEndValidity(), DateStyle.YYYY_MM_DD.getValue()));
			map.put(header[6]+"_"+i,sell.getUserName());
			if(!StringUtils.isEmpty(sell.getControlType())){
				if(sell.getControlType().equals("0")){
					map.put(header[7]+"_"+i,"销控");
				}else{
					map.put(header[8]+"_"+i,"解除销控");
				}
			}
			listMap.add(map);
		}
		return listMap;
	}

}
